package com.example.test.controller;

import com.example.test.model.Course;
import com.example.test.model.Student;
import com.example.test.model.StudentCourse;
import com.example.test.service.CourseService;
import com.example.test.service.StudentCourseService;
import com.example.test.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("studentCourse")
public class StudentCourseController extends BaseController {

    private final StudentService studentService;
    private final CourseService courseService;
    private final StudentCourseService studentCourseService;

    public StudentCourseController(StudentService studentService, CourseService courseService, StudentCourseService studentCourseService) {
        this.studentService = studentService;
        this.courseService = courseService;
        this.studentCourseService = studentCourseService;
    }

    @GetMapping("queryStudentsAndCourses")
    private String queryStudentsAndCourses() {
        session.setAttribute("students", studentService.queryAll());
        session.setAttribute("courses", courseService.queryAll());
        return "redirect:/student_course/add.jsp";
    }

    @PostMapping("create")
    private String create(StudentCourse studentCourse) {
        studentCourseService.create(studentCourse);
        return "redirect:/student_course/list.jsp";
    }
    @GetMapping("queryStudentCourses")
    private String queryStudentCourses() {
        List<Student> students = studentService.queryList("queryStudentCourses", null);
        session.setAttribute("students", students);
        return "redirect:/student_course/list.jsp";
    }

    }

